package game.manager.scene
{
	import flash.utils.Dictionary;

	public class SceneManager
	{
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		// Stuff 
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		private var _stuffNameToStuff:Object = {};
		
		public static function registerStuff(stuffName:String):void
		{
			if (!hasRegisterStuff(stuffName))
			{
				var stuff:StuffProperty = new StuffProperty();
				stuff.name = stuffName;
				
				_instance._stuffNameToStuff[stuff.name] = stuff;
			}
		}
		
		public static function unregisterStuff(stuffName:String):void
		{
			if (hasRegisterStuff(stuffName))
				delete _instance._stuffNameToStuff[stuffName];
		}
		
		public static function hasRegisterStuff(stuffName:String):Boolean
		{
			return _instance._stuffNameToStuff.hasOwnProperty(stuffName);
		}
		
		public static function setStuffProperty(name:String, propertyName:String, propertyValue:*):void
		{
			if (!hasRegisterStuff(name))
				throw new Error("hasn't register name of stuff, should be register before setProperty!");
			
			var stuff:StuffProperty = _instance._stuffNameToStuff[name];
			if (!stuff)
				return;
			
			stuff.setProperty(propertyName, propertyValue);
			
			var monitors:Vector.<Monitor> = _instance.getMonitors(name);
			for each (var monitor:Monitor in monitors)
			{
				monitor.onStuffPropertyChange(propertyName, propertyValue);
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		// Monitor
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		private var _minitorRegisters:Dictionary = new Dictionary(true);
		
		public static function registerMonitor(monitor:Monitor):void
		{
			if (!hasRegisterMonitor(monitor))
			{
				_instance._minitorRegisters[monitor] = true;
			}
		}
		
		public static function unregisterMonitor(monitor:Monitor):void
		{
			if (hasRegisterMonitor(monitor))
			{
				delete _instance._minitorRegisters[monitor];
			}
		}
		
		public static function hasRegisterMonitor(monitor:Monitor):Boolean
		{
			return _instance._minitorRegisters.hasOwnProperty(monitor);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		// Simple logic
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		private function getMonitors(stuffName:String):Vector.<Monitor>
		{
			var result:Vector.<Monitor> = new Vector.<Monitor>();
			
			for (var monitor:* in _minitorRegisters)
			{
				if (monitor.getStuffName() == stuffName)
					result.push(monitor);
			}
			
			return result;
		}
		
		////////:
		private static var _instance:SceneManager = new SceneManager();
	}
}